package com.live.shop.sql;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.live.shop.model.MusicModel;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class MusicListDatabase {
	private static MyDatabaseHelper helper;
	public MusicListDatabase(Context context){
		//创建数据库和表
		helper = new MyDatabaseHelper(context, "user.db",null,3,null);
	}
	@SuppressLint("Range")
	public static List<MusicModel> selectAll() {
		List<MusicModel> list = new ArrayList<>();
		SQLiteDatabase database = helper.getReadableDatabase();
		Cursor cursor = database.rawQuery("select * from music_list ", null);
		if (cursor != null && cursor.getCount() > 0) {
			while (cursor.moveToNext()) {
				MusicModel musicModel = new MusicModel();
				musicModel.setMusicId(cursor.getInt(cursor.getColumnIndex("music_id")));
				musicModel.setMusicName(cursor.getString(cursor.getColumnIndex("music_name")));
				musicModel.setMusicUrl(cursor.getString(cursor.getColumnIndex("music_url")));
				musicModel.setPlayNum(cursor.getInt(cursor.getColumnIndex("play_num")));
				musicModel.setCreateTime(cursor.getString(cursor.getColumnIndex("create_time")));
				list.add(musicModel);
			}
			cursor.close();
		}
		database.close();
		return list;
	}
	//使用播放数量全部音乐
	@SuppressLint("Range")
	public static List<MusicModel> selectUseNum0() {
		List<MusicModel> list = new ArrayList<>();
		SQLiteDatabase database = helper.getReadableDatabase();
		Cursor cursor = database.rawQuery("select * from music_list ", null);
		if (cursor != null && cursor.getCount() > 0) {
			while (cursor.moveToNext()) {
				MusicModel musicModel = new MusicModel();
				musicModel.setMusicId(cursor.getInt(cursor.getColumnIndex("music_id")));
				musicModel.setMusicName(cursor.getString(cursor.getColumnIndex("music_name")));
				musicModel.setMusicUrl(cursor.getString(cursor.getColumnIndex("music_url")));
				musicModel.setPlayNum(0);
				musicModel.setCreateTime(cursor.getString(cursor.getColumnIndex("create_time")));
				list.add(musicModel);
			}
			cursor.close();
		}
		database.close();
		return list;
	}
	// 插入一条数据
	@SuppressLint("SimpleDateFormat")
	public static void insertData(MusicModel musicModel) {
		SQLiteDatabase database = helper.getReadableDatabase();
		ContentValues values = new ContentValues();
		values.put("music_url", musicModel.getMusicUrl());
		values.put("music_name", musicModel.getMusicName());
		values.put("create_time", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		database.insert("music_list", null, values);
		values.clear();
		database.close();
	}
	// 更新数据
	public static void updateData(MusicModel musicModel) {
		SQLiteDatabase database = helper.getReadableDatabase();
		ContentValues values = new ContentValues();
		values.put("music_name", musicModel.getMusicName());
		database.update("music_list", values, "music_id = ?", new String[] {String.valueOf(musicModel.getMusicId())});
		values.clear();
		database.close();
	}
	//仅更改使用次数
	@SuppressLint("Range")
	public static void updatePlayNumData(List<MusicModel> list) {
		SQLiteDatabase database = helper.getReadableDatabase();
		for (MusicModel musicModel:list) {
			database.execSQL("UPDATE music_list SET play_num = play_num + "+musicModel.getPlayNum()+" WHERE music_id = "+musicModel.getMusicId());
		}
		database.close();
	}
	//删除单个
	public static void deleteOne(int music_id) {
		SQLiteDatabase database = helper.getReadableDatabase();
		database.execSQL("delete FROM music_list WHERE music_id = "+music_id);
		database.close();
	}
	//删除所有
	public static void deleteAll() {
		SQLiteDatabase database = helper.getReadableDatabase();
		database.execSQL("delete FROM music_list");
		database.close();
	}
}
